package com.reremouse.rerebbs.dao.impl;

import com.reremouse.rerebbs.dao.IBbsUserDAO;
import com.reremouse.rerebbs.model.BbsUser;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.io.Serializable;
import java.util.List;

public class BbsUserDAO
        implements IBbsUserDAO, Serializable {
    @PersistenceContext
    private EntityManager entityManager;

    public EntityManager getEntityManager() {
        return this.entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public void addBbsUser(BbsUser user) {
        this.entityManager.persist(user);
    }

    public void deleteBbsUser(long id) {
        BbsUser u = (BbsUser) this.entityManager.find(BbsUser.class, Long.valueOf(id));
        this.entityManager.remove(u);
    }

    public List<BbsUser> findAllBbsUser() {
        String q = "Select us from BbsUser us order by  us.regTime desc";
        List<BbsUser> ais = this.entityManager.createQuery(q).getResultList();
        return ais;
    }

    public List<BbsUser> findAllBbsUserOrderByDept() {
        String q = "Select us from BbsUser us order by us.dept, us.enabled desc ,us.activated desc, us.genTime desc";
        List<BbsUser> ais = this.entityManager.createQuery(q).getResultList();
        return ais;
    }

    public BbsUser findBbsUser(long id) {
        BbsUser user = (BbsUser) this.entityManager.find(BbsUser.class, Long.valueOf(id));
        return user;
    }

    public BbsUser findBbsUserByUrn(String urn) {
        String q = "Select us from BbsUser us where us.username='" + urn + "'";
        List<BbsUser> as = this.entityManager.createQuery(q).getResultList();
        if (as.isEmpty()) {
            return null;
        }
        BbsUser ai = (BbsUser) as.get(0);
        return ai;
    }

    public List<BbsUser> findBbsUsersLikeUrn(String urn) {
        String q = "Select us from BbsUser us where us.username like '%" + urn + "%'";
        List<BbsUser> as = this.entityManager.createQuery(q).getResultList();
        return as;
    }

    public BbsUser findBbsUserByEmail(String email) {
        String q = "Select us from BbsUser us where us.email='" + email + "'";

        List<BbsUser> as = this.entityManager.createQuery(q).getResultList();
        if (as.isEmpty()) {
            return null;
        }
        BbsUser ai = (BbsUser) as.get(0);
        return ai;
    }

    public void updateBbsUser(BbsUser u) {
        this.entityManager.merge(u);
    }

    public void enableUser(long id) {
        BbsUser u = (BbsUser) this.entityManager.find(BbsUser.class, Long.valueOf(id));
        u.setEnabled(Boolean.valueOf(true));
        updateBbsUser(u);
    }

    public void disableUser(long id) {
        BbsUser u = (BbsUser) this.entityManager.find(BbsUser.class, Long.valueOf(id));
        u.setEnabled(Boolean.valueOf(false));
        System.out.println(u.getUsername() + " disabled.");
        updateBbsUser(u);
    }

    public void activateUser(long id) {
        BbsUser u = (BbsUser) this.entityManager.find(BbsUser.class, Long.valueOf(id));
        u.setActivated(Boolean.valueOf(true));
        updateBbsUser(u);
    }

    public void enMarkDel(long id) {
        BbsUser u = (BbsUser) this.entityManager.find(BbsUser.class, Long.valueOf(id));
        u.setMarkDel(true);
        updateBbsUser(u);
    }

    public void deMarkDel(long id) {
        BbsUser u = (BbsUser) this.entityManager.find(BbsUser.class, Long.valueOf(id));
        u.setMarkDel(false);
        updateBbsUser(u);
    }
}



/* Location:           F:\jsp源码\蝙蝠论坛系统\rerebbs\WEB-INF\classes\

 * Qualified Name:     com.reremouse.rerebbs.dao.impl.BbsUserDAO

 * JD-Core Version:    0.7.0.1

 */